<script setup>
import {ref, watchEffect} from 'vue';

const props = defineProps({
  value: {
    type: [Number, String],
    default: ''
  },
  modelValue: {
    type: Boolean,
    default: false
  }
})

const emits = defineEmits(['change', 'update:modelValue'])

const checked = ref(false)

watchEffect(() => {
  checked.value = props.modelValue
})


const emitItem = function () {
  checked.value = !checked.value
  let emit = checked.value
  emits('update:modelValue', emit);
  emits('change', emit);
}

</script>

<template>
  <div class="custom-checkbox">
    <label class="checkbox__label" @click="emitItem">
      <checkbox class="checkbox__input" :value="value" :checked="checked"></checkbox>
      <div class="checkbox__inner">
        <div class="checkbox__inner-icon"></div>
      </div>
      <div class="checkbox__text">
        <slot></slot>
      </div>
    </label>
  </div>
</template>

<style scoped lang="scss">
.custom-checkbox {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;

  &.align {
    &-right {
      align-items: flex-end;
    }
  }

  .checkbox__label {
    display: flex;
    flex-direction: row;
    align-items: center;

    .checkbox__text {
      margin: 0 5rpx;
    }


    .checkbox__input {
      display: none;

      &[checked] {
        & + .checkbox__inner {
          background-color: #2979ff;
          border-color: #2979ff;

          .checkbox__inner-icon {
            opacity: 1;
          }
        }
      }
    }

    .checkbox__inner {
      /* #ifndef APP-NVUE */
      flex-shrink: 0;
      box-sizing: border-box;
      /* #endif */
      position: relative;
      width: 16px;
      height: 16px;
      border: 1px solid #ddd;
      border-radius: 4px;
      background-color: #fff;
      z-index: 1;

      .checkbox__inner-icon {
        position: absolute;
        /* #ifdef APP-NVUE */
        top: 2px;
        /* #endif */
        /* #ifndef APP-NVUE */
        top: 1px;
        /* #endif */
        left: 5px;
        height: 8px;
        width: 4px;
        border-right-width: 1px;
        border-right-color: #fff;
        border-right-style: solid;
        border-bottom-width: 1px;
        border-bottom-color: #fff;
        border-bottom-style: solid;
        opacity: 0;
        transform-origin: center;
        transform: rotate(40deg);
      }
    }
  }
}

</style>
